package cn.edu.scau.cmi.linjinyong.mybatis.factory;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SessionFactory {
    private static SqlSession session;

    private SessionFactory(){}

    public static SqlSession getSession(){
        if(session == null) {
            try {
                InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
                SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(stream);
                session = factory.openSession();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return session;
    }
}
